package com.extension.company.common.widget.popuwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.extension.company.admin.R;

/**
 * 作者：zwk
 * 时间：on 2019/3/1 14:37
 * 功能：拍照popwindow
 */
public class PwTakePhoto extends PopupWindow implements View.OnClickListener {

	private Activity context;
	private TextView tv_cancel;
	private OnBtnOnClickListener mOnBtnOnClickListener;
	private TextView takeCamer, openGallery;

	public PwTakePhoto(Activity context) {
		this.context = context;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View takePhotoView = inflater.inflate(R.layout.popwindow_take_photo, null);
		initView(takePhotoView);
		initData();
		setListener();
		this.setContentView(takePhotoView);
		this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
		this.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
		this.setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
		setAnimationStyle(R.style.popwin_anim_style);
		setOutsideTouchable(true);

		takePhotoView.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int top = takePhotoView.findViewById(R.id.ll_first).getTop();

				if (event.getAction() == MotionEvent.ACTION_UP) {
					int y = (int) event.getY();
					if (y < top) {
						PwTakePhoto.this.dismiss();
					}
				}
				return true;
			}
		});
	}

	private void initView(View takePhotoView) {
		tv_cancel = takePhotoView.findViewById(R.id.cancel);
		takeCamer = takePhotoView.findViewById(R.id.takePhoto);
		openGallery = takePhotoView.findViewById(R.id.openPhotos);
	}

	private void initData() {

	}

	private void setListener() {
		tv_cancel.setOnClickListener(this);
		takeCamer.setOnClickListener(this);
		openGallery.setOnClickListener(this);

		this.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				backgroundAlpha(1f);
			}
		});

	}


	/**
	 * 显示popupWindow
	 *
	 * @param parent
	 */
	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			// 以下拉方式显示popupwindow
			this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 20);
		} else {
			PwTakePhoto.this.dismiss();
		}
	}


	/**
	 * 显示
	 */
	public void show() {
		if (!this.isShowing()) {
			hideSoftInputView();
			this.showAtLocation(((ViewGroup) context.findViewById(android.R.id.content)).getChildAt(0), Gravity.CENTER, 0, 0);
			backgroundAlpha(0.7f);
		} else {
			this.dismiss();
		}
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.cancel:
				if (mOnBtnOnClickListener != null) {
					mOnBtnOnClickListener.cancel();
				}
				dismiss();
				break;

			case R.id.takePhoto:
				if (mOnBtnOnClickListener != null) {
					mOnBtnOnClickListener.openCanmer();
				}
				break;

			case R.id.openPhotos:
				if (mOnBtnOnClickListener != null) {
					mOnBtnOnClickListener.openGallerys();
				}
				break;
			default:
				break;
		}

	}

	private void backgroundAlpha(float bgAlpha) {
		if (bgAlpha == 1) {
			//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
			context.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		} else {
			//此行代码主要是解决在华为手机上半透明效果无效的bug
			context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		}
		WindowManager.LayoutParams lp = context.getWindow().getAttributes();
		lp.alpha = bgAlpha; // 0.0~1.0
		context.getWindow().setAttributes(lp); //act 是上下文context
	}


	//强制隐藏软盘
	public void hideSoftInputView() {
		InputMethodManager manager = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE));
		if (context.getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
			if (context.getCurrentFocus() != null)
				manager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}

	public void setBtnOnclickListener(OnBtnOnClickListener onListener) {
		this.mOnBtnOnClickListener = onListener;
	}

	public interface OnBtnOnClickListener {

		void cancel();  // 取消

		void openCanmer(); // 打开相机

		void openGallerys(); // 打开相册
	}
}
